<template>
  <el-dialog
    :title="title"
    :visible="visible"
    width="900px"
    :before-close="closeForm"
    :close-on-click-modal="false"
  >
    <el-form ref="interEdit" label-width="120px">
      <el-card header="基础信息">
        <el-form-item label="事件名">
          {{ inter.InterName }}
        </el-form-item>
        <el-form-item label="事件类型">
          <enumItem
            v-model="inter.UseRange"
            :readonly="true"
            :dic-key="EnumDic.FlowUseRange"
            placeholder="事件类型"
            sys-head="flow"
          />
        </el-form-item>
        <el-form-item label="通信类型" prop="InterType">
          <enumItem
            v-model="inter.InterType"
            :dic-key="EnumDic.FlowInterType"
            :readonly="true"
            placeholder="通信类型"
            sys-head="flow"
          />
        </el-form-item>
      </el-card>
      <el-card v-if="inter.InterType == 1" header="HTTP接口配置" style="margin-top: 10px;">
        <el-form-item label="请求完整URI" prop="Uri">
          {{ http.Uri }}
        </el-form-item>
        <el-form-item v-if="http.Uri && http.Uri.startsWith('https:')" label="SSL协议类型" prop="SslProtocols">
          <el-select
            v-model="http.SslProtocols"
            disabled
            placeholder="SSL协议类型"
          >
            <el-option label="Ssl2" :value="12">Ssl2</el-option>
            <el-option label="Ssl3" :value="48">Ssl3</el-option>
            <el-option label="Tls" :value="192">Tls</el-option>
            <el-option label="Default" :value="240">Default</el-option>
            <el-option label="Tls11" :value="768">Tls11</el-option>
            <el-option label="Tls12" :value="3072">Tls12</el-option>
            <el-option label="Tls13" :value="12288">Tls13</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="解压方式" prop="AutomaticDecompression">
          <el-select
            v-model="http.AutomaticDecompression"
            disabled
            placeholder="解压方式"
          >
            <el-option label="All" :value="-1" />
            <el-option label="GZip" :value="1" />
            <el-option label="Deflate" :value="2" />
            <el-option label="Brotli" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="允许自动重定向" prop="AllowAutoRedirect">
          <el-switch
            v-model="http.AllowAutoRedirect"
            disabled
            placeholder="允许自动重定向"
          />
        </el-form-item>
        <el-form-item v-if="http.AllowAutoRedirect" label="重定向最大数目" prop="MaxAutomaticRedirections">
          <el-input-number
            v-model="http.MaxAutomaticRedirections"
            disabled
            :min="1"
            placeholder="重定向最大数目"
          />
        </el-form-item>
      </el-card>

      <el-card v-else-if="inter.InterType == 0" header="RPC接口配置" style="margin-top: 10px;">
        <el-form-item label="指令" prop="SysDictate">
          {{ rpcConfig.SysDictate }}
        </el-form-item>
        <el-form-item label="目的地">
          <el-select
            v-model="rpcConfig.ToType"
            disabled
            placeholder="目的地"
          >
            <el-option label="服务节点类型" :value="0" />
            <el-option label="固定服务节点" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="rpcConfig.ToType == 0" label="服务节点类型" prop="TypeVal">
          <el-select v-model="rpcConfig.SystemType" disabled clearable placeholder="目的地节点类型">
            <el-option-group
              v-for="group in groups"
              :key="group.Id"
              :label="group.GroupName"
            >
              <el-option
                v-for="item in group.ServerType"
                :key="item.Id"
                :label="item.SystemName"
                :value="item.TypeVal"
              />
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="服务节点" prop="ServerId">
          <el-select v-model="rpcConfig.ServerId" disabled clearable placeholder="目的地节点">
            <el-option v-for="(item) in servers" :key="item.ServerId" :value="item.ServerId" :label="item.ServerName" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用锁" prop="IsEnableLock">
          <el-switch v-model="rpcConfig.IsEnableLock" disabled active-text="启用" inactive-text="不启用" />
        </el-form-item>
        <el-form-item label="负载均衡方案" prop="Transmit">
          <el-input
            v-model="rpcConfig.Transmit"
            maxlength="100"
            readonly
            placeholder="负载均衡方案名"
          />
        </el-form-item>
      </el-card>
      <el-card v-else header="RPC多播配置" style="margin-top: 10px;">
        <el-form-item label="指令" prop="SysDictate">
          <el-input
            v-model="broadcast.SysDictate"
            maxlength="100"
            readonly
            placeholder="指令"
          />
        </el-form-item>
        <el-form-item label="目的地">
          <el-select
            v-model="broadcast.ToType"
            disabled
            placeholder="目的地"
          >
            <el-option label="服务节点类型" :value="0" />
            <el-option label="固定服务节点" :value="1" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="broadcast.ToType == 0" label="服务节点类型" prop="TypeVal">
          <el-select v-model="broadcast.TypeVal" disabled multiple clearable placeholder="目的地节点类型">
            <el-option-group
              v-for="group in groups"
              :key="group.Id"
              :label="group.GroupName"
            >
              <el-option
                v-for="item in group.ServerType"
                :key="item.Id"
                :label="item.SystemName"
                :value="item.TypeVal"
              />
            </el-option-group>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="服务节点" prop="ServerId">
          <el-select v-model="broadcast.ServerId" disabled multiple clearable placeholder="目的地节点">
            <el-option v-for="(item) in servers" :key="item.ServerId" :value="item.ServerId" :label="item.ServerName" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否启用锁" prop="IsEnableLock">
          <el-switch v-model="broadcast.IsEnableLock" disabled active-text="启用" inactive-text="不启用" />
        </el-form-item>
      </el-card>
    </el-form>
  </el-dialog>
</template>

<script>
import { EnumDic } from '@/flow/config/flowConfig'
import * as interApi from '@/flow/api/flowInter'
import * as serverApi from '@/flow/api/server'
export default {
  components: {
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: null
    }
  },
  data() {
    return {
      EnumDic,
      title: '查看流程事件',
      source: {},
      http: {},
      groups: [],
      servers: [],
      rpcConfig: {},
      broadcast: {},
      inter: {}
    }
  },
  computed: {
  },
  watch: {
    visible: {
      handler(val) {
        if (val) {
          this.reset()
        }
      },
      immediate: true
    }
  },
  methods: {
    async loadServer() {
      if (this.servers.length === 0) {
        const list = await serverApi.GetServers()
        this.servers = list
      }
    },
    async loadGroup() {
      if (this.groups.length === 0) {
        const list = await serverApi.Gets()
        this.groups = list
      }
    },
    async reset() {
      this.loadGroup()
      this.loadServer()
      const data = await interApi.Get(this.id)
      this.source = data
      this.title = '查看事件:' + data.InterName
      this.resetForm()
    },
    closeForm() {
      this.$emit('update:visible', false)
      this.$emit('close', false)
    },
    resetHttp() {
      if (this.source.InterType === 1) {
        const config = this.source.InterConfig.HttpConfig
        this.http = {
          Uri: config.Uri,
          AllowAutoRedirect: config.Config.AllowAutoRedirect,
          SslProtocols: config.Config.SslProtocols,
          AutomaticDecompression: config.Config.AutomaticDecompression,
          MaxAutomaticRedirections: config.Config.MaxAutomaticRedirections
        }
      }
    },
    resetRpc() {
      if (this.source.InterType === 0) {
        const config = this.source.InterConfig.RpcConfig
        this.rpcConfig = {
          SysDictate: config.SysDictate,
          ToType: config.SystemType == null || config.SystemType === '' ? 1 : 0,
          SystemType: config.SystemType,
          ServerId: config.ServerId,
          IsEnableLock: config.IsEnableLock,
          Transmit: config.Transmit
        }
      }
    },
    resetBroadcast() {
      if (this.source.InterType === 2) {
        const config = this.source.InterConfig.BroadcastConfig
        this.broadcast = {
          SysDictate: config.SysDictate,
          ToType: config.TypeVal == null || config.TypeVal.length === 0 ? 1 : 0,
          TypeVal: config.TypeVal,
          ServerId: config.ServerId,
          RemoteSet: config.RemoteSet,
          IsOnly: config.IsOnly,
          IsEnableLock: config.IsEnableLock,
          BroadcastType: 0
        }
      }
    },
    resetForm() {
      this.inter = {
        InterName: this.source.InterName,
        InterType: this.source.InterType,
        UseRange: this.source.UseRange
      }
      this.resetHttp()
      this.resetRpc()
      this.resetBroadcast()
    }
  }
}
</script>
